var change_login = new Vue({
    el: "#change",
    data: {
        nickname: handleLocalStorage("get", "nickname")
    },
    methods: {
        check:function () {
            var cart = handleLocalStorage("get", "cart");
            if (!cart) {
                alert("当前没有商品可以结算！请先添加商品至购物车")
            }else {
                window.location.href = "/view/user/checkout.html";
            }
            },
        logout:function() {
            handleLocalStorage("remove", "token");
            handleLocalStorage("remove", "nickname");
            window.location.href = "/";
        }
    }
});
var cart_view = new Vue({
    el: "#cartView",
    data: {
        cartData: ''
    },
    mounted: function () {
        this.getCart();
    },
    methods: {
        getCart: function () {
            var cart = handleLocalStorage("get", "cart");
            if (!cart) {
                return false;
            } else {
                axios({
                    url: "/product/cart",
                    method: "post",
                    data: {
                        "cart": cart
                    },
                    headers: {
                        "Content-Type": "application/json"
                    }
                }).then(function (response) {
                    if (response.data.code == 200) {
                        cart_view.cartData = response.data.data;
                    } else {
                        window.location.href = "/view/other/404.html";
                    }
                }).catch(function (error) {
                    alert("服务器异常");
                });
            }
        },
        getCartNew: function () {
            var cart = handleLocalStorage("get", "cart");
            if (!cart) {
                cart_view.cartData='';
                return false;
            } else {
                axios({
                    url: "/product/cart",
                    method: "post",
                    data: {
                        "cart": cart
                    },
                    headers: {
                        "Content-Type": "application/json"
                    }
                }).then(function (response) {
                    if (response.data.code == 200) {
                        cart_view.cartData = response.data.data;
                    } else {
                        window.location.href = "/view/other/404.html";
                    }
                }).catch(function (error) {
                    alert("服务器异常");
                });
            }
        },
        delCartProduct: function (id) {
            var cart = handleLocalStorage("get", "cart");
            var arr = cart.split(',');
            var arrItem;
            for (var i = 0; i < arr.length-1; i++) {
                arrItem = arr[i].split("-");
                if (arrItem[0] == id) {
                    if (parseInt(arrItem[1])==1){
                       arr.splice(i,1);
                    }else {
                        arrItem[1] = parseInt(arrItem[1]) - 1;
                        arr[i] = arrItem[0] + "-" + arrItem[1];
                    }
                }
            }
            handleLocalStorage("set", "cart", arr);
            cart_view.getCartNew();
        }
    }
});
// var check_product=new Vue({
//    el:"#check",
//    data:"",
//    methods:{
//        check:function () {
//            var cart = handleLocalStorage("get", "cart");
//            if (!cart) {
//               alert("当前没有商品可以结算！请先添加商品至购物车")
//            }else {
//                window.location.href = "/view/user/checkout.html";
//            }
//        }
//    }
// });
